package com.loongnoy.service.impl;

import com.loongnoy.entity.RegisterUser;
import com.loongnoy.entity.User;
import com.loongnoy.mapper.UserMapper;
import com.loongnoy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User save(RegisterUser registerUser) {
        // 构建用户对象
        User user = User.builder()
                .account(registerUser.getAccount())
                .password(registerUser.getPassword())
                .username(registerUser.getUsername())
                .build();
        // 设置注册时间
        user.setRegisteDate(new Date());
        // 转换性别格式
        if ("男".equals(registerUser.getSex())) {
            user.setSex(1);
        } else {
            user.setSex(0);
        }
        // 保存到数据库
        userMapper.insert(user);
        return user;
    }
}
